package fule.com.playandroidkotlin.ui.activity

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.graphics.PixelFormat
import android.view.KeyEvent
import android.view.View
import android.view.View.GONE
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient

import fule.com.playandroidkotlin.base.BaseActivity
import fule.com.kotlindemo.base.BaseView
import fule.com.playandroidkotlin.BuildConfig
import fule.com.playandroidkotlin.R
import fule.com.playandroidkotlin.base.BasePresenter
import fule.com.playandroidkotlin.ui.util.AppConstant
import kotlinx.android.synthetic.main.activity_webview.*
import kotlinx.android.synthetic.main.toolbar.*

/**
 * 作者： njb
 * 时间： 2018/8/11 0011-下午 2:03
 * 描述：
 * 来源：
 */
@Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS", "RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS", "DEPRECATION")
@SuppressLint("Registered")
class WebViewActivity : BaseActivity<BasePresenter<BaseView>>() {
    private lateinit var shareTitle: String
    private lateinit var shareUrl: String
    private var shareId: Int = 0

    override val layoutId: Int
        get() = R.layout.activity_webview

    override fun createPresenter(): BasePresenter<BaseView> {
        return BasePresenter(this)
    }

    override fun initView() {
        initIntent()
        initToolBar()
        initWebViewSettings()
        webView.loadUrl(shareUrl)
    }

    private fun initToolBar() {
        toolbar.run {
            setSupportActionBar(this)
            supportActionBar?.setDisplayHomeAsUpEnabled(true)
            iv_search.visibility = GONE
        }
    }

    private fun initIntent() {
        intent.extras.let {
            shareId = it.getInt(AppConstant.WEBVIEW_ID_KEY)
            shareTitle = it.getString(AppConstant.WEBVIEW_TITLE_KEY)
            shareUrl = it.getString(AppConstant.WEBVIEW_URL_KEY)
        }
    }

    private fun initWebViewSettings() {
        val webSetting = webView.settings
        webSetting.run {
            javaScriptEnabled = true
            javaScriptCanOpenWindowsAutomatically = true
            allowFileAccess = true
            setSupportZoom(false)
            builtInZoomControls = true
            setSupportMultipleWindows(true)
            useWideViewPort = true
            loadWithOverviewMode = true
            layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
            setAppCacheEnabled(true)
            domStorageEnabled = true
            setGeolocationEnabled(true)
            setAppCacheMaxSize(java.lang.Long.MAX_VALUE)
            pluginState = WebSettings.PluginState.ON_DEMAND
            cacheMode = WebSettings.LOAD_NO_CACHE

        }
        // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webView.webChromeClient = WebChromeClient()
        window.setFormat(PixelFormat.TRANSLUCENT)
        webView.overScrollMode = View.OVER_SCROLL_ALWAYS
        WebView.setWebContentsDebuggingEnabled(BuildConfig.DEBUG)
        webView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(webView: WebView, url: String): Boolean {
                webView.loadUrl(url)
                return true
            }

            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                super.onPageStarted(view, url, favicon)
                toolbar.title = getString(R.string.loading)
            }


            override fun onPageFinished(webView: WebView, url: String) {
                super.onPageFinished(webView, url)
                toolbar.title = shareTitle

            }
        }
        progress_bar.max = 100
        webView.webChromeClient = HelloWebViewClient()
    }

    override fun addListener() {

    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }



    private inner class HelloWebViewClient : WebChromeClient() {
        override fun onProgressChanged(view: WebView, newProgress: Int) {
            progress_bar.progress = newProgress

            if (newProgress == 100) {
                progress_bar.visibility = GONE
            }else{
                progress_bar.visibility = View.VISIBLE
            }
            super.onProgressChanged(view, newProgress)
        }
    }

    // mWebView点击返回键返回上一个页面
    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}
